﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HotelManager.Model;
using HotelManager.Core;

namespace HotelManager.DataAccess
{
    class KhachHangAccess
    {
        dbConnection db = new dbConnection();
        public DataTable layToanboTTKhachang()
        {
            return db.executeSelectProcedureQueryNonParameter("select_khachhang");
        }
        //----tim kiem theo ma khach hang
        public DataView timkiemMakh(string Makh)
        {
            DataView dataWiew = layToanboTTKhachang().DefaultView;
            dataWiew.RowFilter = string.Format("Ma_khachhangre like '%{0}%'", Makh);
            return dataWiew;
        }
        //----tim kiem theo ten khach hnag
        public DataView timkiemTenkh(string Tenkh)
        {
            DataView dataWiew = layToanboTTKhachang().DefaultView;
            dataWiew.RowFilter = string.Format("Ten_khachhang like '%{0}'", Tenkh);
            return dataWiew;
        }
        //
        public DataTable timkhachhangtheoma(string makh)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@Ma_kh",SqlDbType.Char,10);
            ma.Value = makh;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("timkiem_khachhang", listParam);
        }
        //----them khach hang
        public int themKhachHang(KhachHang kh)
        {
            if ((timkhachhangtheoma(kh.MaKhach1).Rows.Count > 0))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[8];
                SqlParameter makh = new SqlParameter("@Ma_kh", SqlDbType.Char, 10);
                makh.Value = kh.MaKhach1;
                listParam[0] = makh;
                listParam[1] = new SqlParameter("@Ten_kh", kh.TenKhach1);
                listParam[2] = new SqlParameter("@Ngay_sinh", kh.NgaySinh1);
                listParam[3] = new SqlParameter("@Quoc_tinh", kh.QuocTich1);
                listParam[4] = new SqlParameter("@Dien_thoai", kh.SDT1);
                listParam[5] = new SqlParameter("@Gioi_tinh", kh.GioiTinh1);
                listParam[6] = new SqlParameter("@Dia_chi", kh.DiaChi1);
                listParam[7] = new SqlParameter("@Cmt", kh.CMND1);
                db.executeInsertProcedureQuery("intsert_khachhang", listParam);
                return 1;
            }
        }
        //----sua khach hang
        public int suaKhachHang(KhachHang kh)
        {
            if ((timkhachhangtheoma(kh.MaKhach1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[8];
                SqlParameter makh = new SqlParameter("@Ma_kh", SqlDbType.Char, 10);
                makh.Value = kh.MaKhach1;
                listParam[0] = makh;
                listParam[1] = new SqlParameter("@Ten_kh", kh.TenKhach1);
                listParam[2] = new SqlParameter("@Ngay_sinh", kh.NgaySinh1);
                listParam[3] = new SqlParameter("@Quoc_tinh", kh.QuocTich1);
                listParam[4] = new SqlParameter("@Dien_thoai", kh.SDT1);
                listParam[5] = new SqlParameter("@Gioi_tinh", kh.GioiTinh1);
                listParam[6] = new SqlParameter("@Dia_chi", kh.DiaChi1);
                listParam[7] = new SqlParameter("@Cmt", kh.CMND1);
                db.executeInsertProcedureQuery("intsert_khachhang", listParam);
                return 1;
            }
        }
        //----xoa khach hang
        public int xoakh(KhachHang kh)
        {
            if ((timkhachhangtheoma(kh.MaKhach1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                SqlParameter makh = new SqlParameter("@Ma_kh", SqlDbType.Char, 10);
                makh.Value = kh.MaKhach1;
                listParam[0] = makh;
                db.executeDeleteProcedureQuery("delete_khachhang", listParam);
                return 1;
            }
        }
    }
}